﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.DirectoryServices;


namespace SuraQR.Login
{
    
    class LoginLDAP
    {
        public bool GetUserInfo(out List<string> userInformation, string userName)
        {
            userInformation = new List<string>();
            var valueReturn = false;
            try
            {

                const string pathNameDomain = "LDAP://CHANGE_DOMAIN_NAME_HERE";

                var direcotyEntry = new DirectoryEntry(pathNameDomain);


                var directorySearcher = new DirectorySearcher(direcotyEntry)
                {
                    Filter = "(&(objectClass=user)(sAMAccountName=" + userName + "))"
                };

                var searchResults = directorySearcher.FindAll();

                valueReturn = searchResults.Count > 0;

                foreach (SearchResult searchResult in searchResults)
                {

                    foreach (var valueCollection in searchResult.Properties.PropertyNames)
                    {
                        userInformation.Add(valueCollection.ToString() + " = " + searchResult.Properties[valueCollection.ToString()][0].ToString());
                    }
                }

                direcotyEntry.Dispose();
                directorySearcher.Dispose();
                searchResults.Dispose();
            }
            catch (Exception e)
            {
                Debug.WriteLine("Error : " + e.ToString());
            }
            return valueReturn;
        }    
    }
}
